package com.raptor.mobile.samples;

import java.util.Arrays;
import java.util.List;

import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ItemDecoration;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mActivityListView;
    private QueryActivityTask mTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivityListView = (RecyclerView) findViewById(R.id.recyclerView);
        mTask = new QueryActivityTask();
        mTask.execute();
    }


    class QueryActivityTask extends AsyncTask<Object, Object, List<ActivityInfo>> {

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected List doInBackground(Object... params) {
            try {
                ActivityInfo[] data = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_ACTIVITIES).activities;
                return Arrays.asList(data);
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(List<ActivityInfo> activityInfos) {
            if (activityInfos == null || activityInfos.size() == 0) {
                Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_SHORT);
            } else {
                mActivityListView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                mActivityListView.setAdapter(new ActivityAdapter(MainActivity.this, activityInfos));
            }
        }
    }

    class Divider extends  RecyclerView.ItemDecoration{
        @Override
        public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
            super.onDrawOver(c, parent, state);
        }
    }
}
